package com.example.wishstar.view.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.content.Intent;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.wishstar.MainActivity;
import com.example.wishstar.R;
import com.example.wishstar.databinding.ActivityAddHabitBinding;
import com.example.wishstar.viewmodel.AddHabitViewModel;

public class AddHabitActivity extends AppCompatActivity implements AddHabitViewModel.AddHabitCallBack{

    private static final String DEFAULT_FREQUENCY = "1111100";
    private static final int FREQUENCY_LENGTH = 7;
    private static final int HABIT_ICON_NUM = 16;
    private String TAG = "AddHabitActivity";
    private ActivityAddHabitBinding activityAddHabitBinding;
    private AddHabitViewModel addHabitViewModel;

    private GradientDrawable frequencyIconBackground[] = new GradientDrawable[FREQUENCY_LENGTH];
    private GradientDrawable habitIconBackground[] = new GradientDrawable[HABIT_ICON_NUM];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        activityAddHabitBinding = DataBindingUtil.setContentView(this, R.layout.activity_add_habit);
        addHabitViewModel = new AddHabitViewModel(this);
        activityAddHabitBinding.setAddHabitViewModel(addHabitViewModel);

        addHabitViewModel.setAddHabitCallBack(this);

        TextView title = findViewById(R.id.post_title);
        title.setText(getString(R.string.habit_post_title));

        for (int i = 0; i < FREQUENCY_LENGTH; i++) {
            TextView textView = findTextViewByPostion(i + 1);
            frequencyIconBackground[i] = (GradientDrawable) textView.getBackground();
        }
        setFrequencyIconColor(DEFAULT_FREQUENCY);

        for (int i = 0; i < HABIT_ICON_NUM; ++i) {
            ImageView imageView = findByPostion(i + 1);
            habitIconBackground[i] = (GradientDrawable) imageView.getBackground();
        }
        clearHabitIconColor();

        ImageView backImage = findViewById(R.id.image_title_back);
        backImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(AddHabitActivity.this, MainActivity.class);
                startActivity(intent);
                clearFrequencyIconColor();
                clearHabitIconColor();
                finish();
            }
        });

        Button button = findViewById(R.id.button_title_post);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addHabitViewModel.post();
                //Toast.makeText(AddWishActivity.this, "发布成功", Toast.LENGTH_SHORT).show();
                clearFrequencyIconColor();
                clearHabitIconColor();
                finish();
            }
        });
    }

    private void setFrequencyIconColor(String frequency) {
        for (int i = 0; i < FREQUENCY_LENGTH; ++i) {
            if (frequency.charAt(i) == '1') {
                frequencyIconBackground[i].setColor(getColor(R.color.yellow_light));
            }
            else {
                frequencyIconBackground[i].setColor(getColor(R.color.green_transparent));
            }
        }
    }

    private void clearHabitIconColor() {
        for (int i = 0; i < HABIT_ICON_NUM; ++i) {
            habitIconBackground[i].setColor(getColor(R.color.green_transparent));
        }
    }

    private void clearFrequencyIconColor() {
        for (int i = 0; i < FREQUENCY_LENGTH; ++i) {
            frequencyIconBackground[i].setColor(getColor(R.color.green_transparent));
        }
    }

    private TextView findTextViewByPostion(int postion) {
        switch (postion) {
            case 1:
                return findViewById(R.id.add_new_habite_frequency_icon1);
            case 2:
                return findViewById(R.id.add_new_habite_frequency_icon2);
            case 3:
                return findViewById(R.id.add_new_habite_frequency_icon3);
            case 4:
                return findViewById(R.id.add_new_habite_frequency_icon4);
            case 5:
                return findViewById(R.id.add_new_habite_frequency_icon5);
            case 6:
                return findViewById(R.id.add_new_habite_frequency_icon6);
            case 7:
                return findViewById(R.id.add_new_habite_frequency_icon7);
        }
        return findViewById(R.id.add_new_habite_frequency_icon7);
    }

    private ImageView findByPostion(int postion) {
        switch (postion) {
            case 1:
                return findViewById(R.id.add_new_habite_image_icon1);
            case 2:
                return findViewById(R.id.add_new_habite_image_icon2);
            case 3:
                return findViewById(R.id.add_new_habite_image_icon3);
            case 4:
                return findViewById(R.id.add_new_habite_image_icon4);
            case 5:
                return findViewById(R.id.add_new_habite_image_icon5);
            case 6:
                return findViewById(R.id.add_new_habite_image_icon6);
            case 7:
                return findViewById(R.id.add_new_habite_image_icon7);
            case 8:
                return findViewById(R.id.add_new_habite_image_icon8);
            case 9:
                return findViewById(R.id.add_new_habite_image_icon9);
            case 10:
                return findViewById(R.id.add_new_habite_image_icon10);
            case 11:
                return findViewById(R.id.add_new_habite_image_icon11);
            case 12:
                return findViewById(R.id.add_new_habite_image_icon12);
            case 13:
                return findViewById(R.id.add_new_habite_image_icon13);
            case 14:
                return findViewById(R.id.add_new_habite_image_icon14);
            case 15:
                return findViewById(R.id.add_new_habite_image_icon15);
            case 16:
                return findViewById(R.id.add_new_habite_image_icon16);
        }
        return findViewById(R.id.add_new_habite_image_icon16);
    }

    @Override
    public void choiceFrequencyCallback(String frequency) {
        for (int i = 0; i < FREQUENCY_LENGTH; ++i) {
            if (frequency.charAt(i) == '1') {
                frequencyIconBackground[i].setColor(getColor(R.color.yellow_light));
            }
            else {
                frequencyIconBackground[i].setColor(getColor(R.color.green_transparent));
            }
        }
    }

    @Override
    public void chocieHabitIconCallback(int postion) {
        for (int i = 0; i < HABIT_ICON_NUM; ++i) {
            if (i + 1 == postion) {
                habitIconBackground[i].setColor(getColor(R.color.yellow_light));
            }
            else {
                habitIconBackground[i].setColor(getColor(R.color.green_transparent));
            }
        }
    }
}